package com.lllmark.lllmarkDemo.service.serviceImpl;

import com.lllmark.lllmarkDemo.Utils.MyStringUtils;
import com.lllmark.lllmarkDemo.constans.Constans;
import com.lllmark.lllmarkDemo.entity.dto.UserDTO;
import com.lllmark.lllmarkDemo.entity.po.User;
import com.lllmark.lllmarkDemo.entity.vo.UserVO;
import com.lllmark.lllmarkDemo.mapper.UserMapper;
import com.lllmark.lllmarkDemo.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author yuxiangZhang
 * @data 2024/5/11
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserVO selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<UserVO> slectAll(UserDTO userDTO) {

        Integer pageSize = userDTO.getPageSize();
        Integer page = userDTO.getPage();
        page = page * pageSize - pageSize;
        userDTO.setPage(page);
        return userMapper.selectAll(userDTO);
    }

    /**
     * 获取用户数量
     * @return
     */
    @Override
    public Integer getUserCounts() {
        return userMapper.getUserCounts();
    }


    public Integer update(UserDTO userDTO) {

        Integer flag = userMapper.update(userDTO);

        return flag;
    }

    @Override
    public Integer deleteById(Integer id) {
        Integer flag = userMapper.delete(id);
        return flag;
    }

    @Override
    public Integer insert(User user) {
        user.setJoinTime(new Date());
        user.setUserId(new MyStringUtils().getUserName(Constans.USER_ID_LENGTH_DEFAULT));
        user.setUpdateTime(new Date());
        return userMapper.insert(user);
    }
}
